package cn.k.controller;

import cn.k.domain.Employee;
import cn.k.domain.Notice;
import cn.k.qo.NoticeQueryObject;
import cn.k.qo.QueryObject;
import cn.k.service.INoticeService;
import cn.k.util.RequirePermission;
import cn.k.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;

    //处理分页查询页面请求
    @RequestMapping("/list")
    @RequirePermission(name = "公告查询",expression = "notice:list")
    public String list(@ModelAttribute("qo") NoticeQueryObject qo, Model model){
        PageInfo<Notice> pageInfo = noticeService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        Employee employee = UserContext.getEmployee();
        model.addAttribute("employee",employee);
        return "notice/list";
    }


    //处理删除请求
    @RequirePermission(name = "公告删除",expression = "notice:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id!=null){
            noticeService.delete(id);
        }
        return "redirect:/notice/list";
    }


    //处理添加或者更新的请求
    @RequirePermission(name ="公告编辑",expression = "notice:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Notice notice){
        //判断有没有id
        if (notice.getId()==null){
            noticeService.insert(notice);
        }else {
            noticeService.update(notice);
        }
        return "redirect:/notice/list";
    }

    //员工查看页面
    @RequirePermission(name ="公告查看",expression = "notice:show")
    @RequestMapping("/show")
    public String show(Long id,Model model){
        Notice notice = noticeService.get(id);
        //如果拿到的状态是未读,才更改状态
        if (notice.getReadId()==0){
            notice.setReadId(1); //设置已读状态
            notice.setName(UserContext.getEmployee().getName());
            noticeService.update(notice);
        }
        model.addAttribute("notice",notice);
        return "notice/show";
    }

    //页面查看2
    @RequirePermission(name ="公告查看",expression = "notice:show")
    @RequestMapping("/showTwo")
    public String showTwo(Long id,Model model){
        //获取当前用户id
        Long employeeId = UserContext.getEmployee().getId();
        //查出主表与中间表对应的状态
        Notice notice=noticeService.queryByReadRelation(id,employeeId);
        int status = notice.getStatus(); //获取当前用户查询公告的状态
        if (status==0){
            noticeService.updateReadRelation(id,employeeId,status);
        }
        //更新状态
        model.addAttribute("notice",notice);
        return "notice/show";
        }




   //更新发布状态
    @RequestMapping("/updateReleaseStatus")
    public String updateReleaseStatus(Long id,int releaseStatus){
        Notice notice = noticeService.get(id);
        if (notice.getReleaseStatus()==0){
            noticeService.updateReleaseStatus(id,releaseStatus);
        }
        return "redirect:/notice/list";
    }

}
